package ynu.lcy.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import ynu.lcy.dto.business.BusinessQueryDTO;
import ynu.lcy.dto.business.BusinessRequestDTO;
import ynu.lcy.elememicroservicecommon.annotation.AuthCheck;
import ynu.lcy.elememicroservicecommon.constant.UserConstant;
import ynu.lcy.elememicroservicecommon.utils.BaseResponse;
import ynu.lcy.elememicroservicecommon.utils.ResultUtils;

import ynu.lcy.entity.Business;
import ynu.lcy.service.BusinessService;
import ynu.lcy.vo.BusinessVO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "商家接口")
@RequestMapping("/business")
public class BusinessController {
    @Resource
    private BusinessService service;

    @Resource
    private ModelMapper modelMapper;

    //获取商家列表
    @PostMapping("/list")
    @ApiOperation("获取商家列表")
    public BaseResponse<List<BusinessVO>> list(@RequestBody BusinessQueryDTO dto) {
        List<Business> list = service.listByKeyword(dto);
        List<BusinessVO> vos = new ArrayList<>();
        //转标签
        list.forEach(e ->{
            List<String> tags = new ArrayList<>();
            e.getBusinessOrderTypes().forEach(f ->tags.add(f.getOrderType().getLabel()));
            BusinessVO vo = modelMapper.map(e, BusinessVO.class);
            vo.setTags(tags);
            vos.add(vo);
        });
        return ResultUtils.success(vos);
    }

    //根据类型获取商家列表
    @GetMapping("/listByType/{type}")
    @ApiOperation("根据类型获取商家列表")
    @Transactional
    public BaseResponse<List<BusinessVO>> listByType(@ApiParam("type") @PathVariable("type") Long id) {
        List<BusinessVO> vos = service.listByType(id).stream()
                .map(e -> modelMapper.map(e, BusinessVO.class)).toList();
        return ResultUtils.success(vos);
    }

    //根据id获取
    @GetMapping("/getById/{id}")
    @ApiOperation("根据id获取")
    @Transactional
    public BaseResponse<BusinessVO> getById(@ApiParam("id") @PathVariable("id") Long id) {
        Business business = service.getById(id);
        BusinessVO vo = modelMapper.map(business, BusinessVO.class);
        return ResultUtils.success(vo);
    }

    /**
     * 添加商家
     *
     * @param dto 请求实体
     * @return
     * @role admin
     */
    @PostMapping("/admin/add")
    @ApiOperation("添加商家")
//    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<BusinessVO> add(@ApiParam("请求实体") BusinessRequestDTO dto) {
        Business business = service.add(dto);
        BusinessVO vo = modelMapper.map(business, BusinessVO.class);
        return ResultUtils.success(vo);
    }


}
